﻿using System;
using System.Collections.Specialized;
using Newtonsoft.Json;

namespace CometDocs
{
    internal partial class Client
    {
        /// <summary>
        /// Create a new folder: https://www.cometdocs.com/developer/apiDocumentation#method-createFolder
        /// </summary>
        /// <param name="token">A valid authentication token.</param>
        /// <param name="parent">The folder in which the new folder should be created.</param>
        /// <param name="name">The name of the new folder.</param>
        /// <returns>A FolderInfo instance representing the newly created folder.</returns>
        public FolderInfo CreateFolder(AuthenticationToken token, FolderInfo parent, String name)
        {
            NameValueCollection @params = new NameValueCollection(3);
            @params.Add("token", token.Value);
            @params.Add("folderId", parent.ID.ToString());
            @params.Add("name", name);

            byte[] result = webClient.UploadValues(APIRoot + "createFolder", @params);

            string json = encoding.GetString(result);

            CreateFolderResponse response = JsonConvert.DeserializeObject<CreateFolderResponse>(json);

            CheckAndThrow(response);

            return response.Folder;
        }
    }
}
